############################################################
# Lajevardi and Spangler                                   #
# Journal: PS                                              #
# R Replication Code                                       #
# Tested on R version 4.0.3                                #
############################################################

rm(list=ls())

# working directory ----
wd <- "~/Dropbox/Personal/MSU/Projects/Lajevardi and Spangler LSS/rep_files/"

# setup ----
install.packages("tidyverse")
install.packages("zoo")
install.packages("lubridate")

# libraries ----
library(tidyverse)
library(zoo)
library(lubridate)

# volume graph ----
df_vol <- read_csv(file.path(wd, "df_vol.csv")) %>%
  mutate(month_year = as.yearmon(month_year))

ggplot(df_vol, aes(month_year, tweetsthatmonth_st)) +
  geom_bar(stat="identity", color = "#525252", fill = "#6f6f6f") + 
  theme_classic() + 
  xlab("") + 
  ylab("Messages Referencing Muslims/Islam per Month per Legislator")+ 
  labs(caption = str_wrap("Standardized by the total number of legislators in that category during that time period regardless of whether they sent a message referencing Muslims and/or Islam.", 120))+
  theme(legend.position = "none", 
        plot.caption = element_text(hjust = 0.5, color = "darkgray", face = "italic", size = 8), 
        axis.ticks = element_blank(), 
        axis.text = element_text(size = 8), 
        axis.title = element_text(size = 10), 
        strip.text = element_text(size = 10)) +  
  scale_x_continuous(breaks = seq(2011, 2017, 1))+ 
  facet_grid(cols = vars(race_di), 
             rows = vars(party)) 


# sentiment graph ----

df_sent <- read_csv(file.path(wd, "df_sent.csv")) %>%
  mutate(month_year = as.yearmon(month_year))

ggplot(df_sent, aes(month_year, average_sent)) +
  geom_bar(stat="identity", color = "#525252", fill = "#6f6f6f") + 
  theme_classic() + 
  xlab("") + 
  ylab("Mean Muslim/Islam Message Sentiment* by Month") +
  labs(caption = str_wrap("*Sentiment values are measured by calculating positive words minus the negative words in each tweet for each of the three primary sentiment dictionaries: Hiu & Lu, AFinn, and NRC. The dictionary-specific sentiment scores for each tweet are then averaged together. The values on the y-axis represent the monthly mean of these tweet-level mean sentiment scores.", 120)) +
  theme(legend.position = "none", 
        plot.caption = element_text(hjust = 0.5, color = "darkgray", face = "italic", size = 8), 
        axis.ticks = element_blank(), 
        axis.text = element_text(size = 8), 
        axis.title = element_text(size = 10), 
        strip.text = element_text(size = 10)) + 
  scale_x_continuous(breaks = seq(2011, 2017, 1)) + 
  facet_grid(rows = vars(party), 
             cols = vars(race_di))






